/**
 * 文件名称:  TBcOrganController.java
 * 文件序号:  序号
 * 内容描述:  文件简单描述，多行换行书写
 * 版权声明:  山东鸿蝶信息技术有限公司
 * 创建用户:  zhangkai
 * 创建日期:  2017/1/17 15:28
 * 修改用户:  无
 * 修改日期:  无
 * 修改说明:  无
 * 文件版本:  V1.0
 * 文件路径:
 */

package com.hongdee.rbac.dept.controller;


import com.hongdee.base.entity.ResultEntity;
import com.hongdee.common.util.EntityUtils;
import com.hongdee.common.util.TokenUtil;
import com.hongdee.rbac.dept.entity.TBcOrgan;
import com.hongdee.rbac.dept.service.ITBcOrganService;
import com.hongdee.rbac.dept.service.impl.TBcOrganServiceImpl;
import com.sun.javafx.collections.MappingChange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 名  称:TBcOrganController
 * 类  型: class
 * 描  述: 用一句话描述类的作用
 * 路  径:com.hongdee.rbac.dept.controller
 * 作  者:zhangkai
 * 日  期:2017/1/17 15:28
 */
@RestController
@RequestMapping("/rbac/dept")
public class TBcOrganController {
    @Autowired
    private ITBcOrganService organService;

    /**
     * 方法名称: page
     * 方法描述: 分页查询变更历史
     * 参数名称: String uid 登录用户id
     * 参数名称: String token 登录用户 token
     * 参数名称: String start 记录起始行数
     * 参数名称: String limit 分页大小
     * 返回内容: 分页数据:
     * 异常说明:
     * 创建用户: 张国凯
     * 创建时间:
     * 修改用户: 张国凯
     * 修改时间:
     * 修改内容:
     */

    @RequestMapping("/page")
    @ResponseBody
    public Map page(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token,
                    @RequestParam(name = "start", defaultValue = "0") String start,
                    @RequestParam(name = "limit", defaultValue = "50") String limit,
                    @RequestParam(name = "id", defaultValue = "") String pId,
                    @RequestParam(name = "searchValue", defaultValue = "") String searchValue) throws Exception {
        Map map = new HashMap();
        boolean flag = organService.getChildDeptListByCompany(uid, pId);
        map = organService.page(start, limit, pId, searchValue, uid);
        //验证选中的部门，其向上寻找，数据权限中是否有其公司 如有则显示新增，如没有 隐藏
        map.put("success", flag);
        map.put("msg", "分页查询成功！");
//        boolean addflag = organService.getChildDeptListByCompany(uid, pId);
        map.put("addFlag", flag);

        return map;
    }

    @RequestMapping("/deptPage")
    @ResponseBody
    public Map deptPage(@RequestParam(name = "uid", defaultValue = "") String uid,
                        @RequestParam(name = "token", defaultValue = "") String token,
                        @RequestParam(name = "start", defaultValue = "0") String start,
                        @RequestParam(name = "limit", defaultValue = "50") String limit,
                        @RequestParam(name = "id", defaultValue = "") String pId,
                        @RequestParam(name = "searchValue", defaultValue = "") String searchValue) throws Exception {


        Map map = organService.page(start, limit, pId, searchValue, uid);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }

    /**
     * 方法名称: info
     * 方法描述: 查询组织详情
     * 参数名称: uid 登录用户 ID
     * 参数名称: token 登录用户 Token
     * 参数名称: id 主键
     * 返回内容:
     * 异常说明:
     * 创建用户:
     * 创建时间:
     * 修改用户:
     * 修改时间:
     * 修改内容:
     */
    @RequestMapping("/info")
    @ResponseBody
    public ResultEntity info(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        if (StringUtils.isEmpty(id)) {
            throw new Exception("请选择组织");
        }
        Map map = organService.info(id);
        res.setData(map);
        res.setSuccess(true);
        res.setMsg("查询详情成功");
        return res;
    }

    /**
     * 查看详情
     * @param uid
     * @param token
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/newInfo")
    @ResponseBody
    public ResultEntity newInfo(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        if (StringUtils.isEmpty(id)) {
            throw new Exception("请选择组织");
        }
        Map map = organService.newInfo(id);
        res.setData(map);
        res.setSuccess(true);
        res.setMsg("查询详情成功");
        return res;
    }

    /**
     * 兼营品牌+自营品牌下拉列表
     * @return
     * @throws Exception
     */
    @RequestMapping("/combo")
    @ResponseBody
    public List<Map> combo(@RequestParam(name = "uid", defaultValue = "") String uid,
                          @RequestParam(name = "token", defaultValue = "") String token
    ) throws Exception {
        return this.organService.comboBrand(uid);
    }

    /**
     * 方法名称:start
     * 方法描述:启用
     * 参数名称: uid 登录用户 ID
     * 参数名称: token 登录用户 Token
     * 参数名称: id 主键
     * 返回内容:
     * 异常说明:
     * 创建用户:
     * 创建时间:
     * 修改用户:
     * 修改时间:
     * 修改内容:
     */
    @RequestMapping("/start")
    @ResponseBody
    public ResultEntity start(@RequestParam(name = "uid", defaultValue = "") String uid,
                              @RequestParam(name = "token", defaultValue = "") String token,
                              @RequestParam(name = "id", defaultValue = "0") String id) throws Exception {
        if (StringUtils.isEmpty(id)) {
            throw new Exception("请选择组织");
        }
        ResultEntity res = new ResultEntity();
        organService.update(id, "1");
        res.setSuccess(true);
        res.setMsg("启用成功");
        return res;
    }

    /**
     * 方法名称: stop
     * 方法描述: 停用
     * 参数名称: uid 登录用户 ID
     * 参数名称: token 登录用户 Token
     * 参数名称: id 主键
     * 返回内容:
     * 异常说明:
     * 创建用户:
     * 创建时间:
     * 修改用户:
     * 修改时间:
     * 修改内容:
     */
    @RequestMapping("/stop")
    @ResponseBody
    public ResultEntity stop(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "0") String id) throws Exception {
        if (StringUtils.isEmpty(id)) {
            throw new Exception("请选择组织");
        }
        ResultEntity res = new ResultEntity();
        organService.update(id, "0");
        res.setSuccess(true);
        res.setMsg("停用成功！");
        return res;
    }


    /**
     * 方法名称:save
     * 方法描述: 保存数据
     * 参数名称: uid 登录用户 ID
     * 参数名称: token 登录用户 Token
     * 参数名称: TBcOrgan organ 要保存的组织实体
     * 返回内容:ResultEntity
     * 异常说明:
     * 创建用户:
     * 创建时间:
     * 修改用户:
     * 修改时间:
     * 修改内容:
     */
    @RequestMapping("/save")
    @ResponseBody
    public ResultEntity save(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestBody TBcOrgan organ) throws Exception {
        //判断必填项,
        EntityUtils.valid(organ);
        String organId=organ.getId();
        TBcOrgan bcOrgan = organService.saveOrUpdate(organ);
        if ("".equals(organId)) {
            organService.saveRoleDataOrg(bcOrgan, uid);
        }
        ResultEntity res = new ResultEntity();
        res.setData(bcOrgan);
        res.setSuccess(true);
        res.setMsg("保存成功！");
        return res;
    }

    @RequestMapping("/newSave")
    @ResponseBody
    public ResultEntity newSave(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestBody Map organ) throws Exception {
        //判断必填项,
        //EntityUtils.valid(organ);
        String organId=organ.get("id").toString();
        TBcOrgan bcOrgan = organService.save(organ);
        if ("".equals(organId)) {
            organService.saveRoleDataOrg(bcOrgan, uid);
        }
        ResultEntity res = new ResultEntity();
        res.setData(bcOrgan);
        res.setSuccess(true);
        res.setMsg("保存成功！");
        return res;
    }

    /**
     * 方法名称: tree
     * 方法描述: 组织机构树型查询
     * 参数名称:
     * 返回内容: 无
     * 异常说明: 抛出异常Exception
     * 创建用户: MissJin
     * 创建时间: 2017/1/21/17:32
     * 修改用户:
     * 修改时间:
     * 修改内容:
     */
    @RequestMapping("/tree")
    @ResponseBody
    public ResultEntity tree(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "root", defaultValue = "") String root,
//                           @RequestParam(name = "level", defaultValue = "0") int level,
                             @RequestParam(name = "type", defaultValue = "") String type) throws Exception {

        String companyId = root;
        if (StringUtils.isEmpty(root)) {
            companyId = TokenUtil.getCompanyId(uid);
        }
        //树形结构数组
        List treeList = new ArrayList();
        //取下级节点
        List childList = this.organService.orgTree(companyId, "0", uid);
        //取当前节点，做为根节点
        Map treeParent = this.organService.info(companyId);
        treeParent.put("child", childList);
        treeList.add(treeParent);
        ResultEntity res = new ResultEntity();
        res.setDataList(treeList);
        res.setSuccess(true);
        res.setMsg("查询组织机构");
        return res;
    }

    @RequestMapping("/detpTree")
    @ResponseBody
    public ResultEntity detpTree(@RequestParam(name = "uid", defaultValue = "") String uid,
                                 @RequestParam(name = "token", defaultValue = "") String token,
                                 @RequestParam(name = "root", defaultValue = "") String root,
//                           @RequestParam(name = "level", defaultValue = "0") int level,
                                 @RequestParam(name = "type", defaultValue = "") String type) throws Exception {

        String companyId = root;
        if (StringUtils.isEmpty(root)) {
            companyId = TokenUtil.getCompanyId(uid);

        }
        //树形结构数组
        List treeList = new ArrayList();
        //取下级节点
        List childList = this.organService.tree(companyId, "0");
        //取当前节点，做为根节点
        Map treeParent = this.organService.info(companyId);
        treeParent.put("child", childList);
        treeList.add(treeParent);
        ResultEntity res = new ResultEntity();
        res.setDataList(treeList);
        res.setSuccess(true);
        res.setMsg("查询组织机构");
        return res;
    }


    /**
     * @param uid   登录用户 id
     * @param token 登录用户 Token
     * @param root  要查询的组织根节点
     * @param type  要查询的组织类型
     * @return Map
     * @throws Exception 基本异常
     * @desc 品牌编码转换
     * @author 张国凯
     */
    @RequestMapping("/convert")
    @ResponseBody
    public Map convert(@RequestParam(name = "uid", defaultValue = "") String uid,
                       @RequestParam(name = "token", defaultValue = "") String token,
                       @RequestParam(name = "root", defaultValue = "0") String root,
                       @RequestParam(name = "type", defaultValue = "1") String type) throws Exception {
        //根据 UID 取当前用户组织信息) throws Exception {
        return this.organService.convert(root, type);
    }

    /**
     * @param uid   登录用户 id
     * @param token 登录用户 Token
     * @param type  0,仅查询部门，1 查询部门+用户
     * @return
     * @throws Exception
     * @desc 查询组织机构树
     */
    @RequestMapping("/orgTree")
    @ResponseBody
    public ResultEntity orgTree(@RequestParam(name = "uid", defaultValue = "") String uid,
                                @RequestParam(name = "token", defaultValue = "") String token,
                                @RequestParam(name = "type", defaultValue = "1") String type) throws Exception {
        /**
         * 根据 UID 获取其所在公司 ID,只允许查询其下属树
         * **/
        String companyId = TokenUtil.getDeptId(uid);

        List ls = this.organService.orgOrleDataTree(companyId, type, uid);
        //树形结构数组
        List treeList = new ArrayList();
        //取下级节点
        //取当前节点，做为根节点
        Map treeParent = this.organService.info(companyId);
        treeParent.put("child", ls);
        treeList.add(treeParent);
        ResultEntity res = new ResultEntity();
        res.setDataList(treeList);
        res.setSuccess(true);
        res.setMsg("查询组织机构");
        return res;
    }

    /**
     * 公司下的部门
     *
     * @param uid
     * @param token
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/depyList")
    @ResponseBody
    public ResultEntity depyList(@RequestParam(name = "uid", defaultValue = "") String uid,
                         @RequestParam(name = "token", defaultValue = "") String token,
                         @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        List list = this.organService.tree1(TokenUtil.getCompanyId(uid), "2");
        res.setDataList(list);
        res.setSuccess(true);
        res.setMsg("查询成功");
        return res;
    }

    /**
     * 获得所有的公司信息
     * @param uid
     * @param token
     * @return
     * @throws Exception
     */
    @RequestMapping("/all")
    @ResponseBody
    public Map deptAll(@RequestParam(name = "uid", defaultValue = "") String uid,
                       @RequestParam(name = "token", defaultValue = "") String token,
                       @RequestParam(name = "area", defaultValue = "") String area,
                       @RequestParam(name = "brand", defaultValue = "") String brand) throws Exception {
        Map map =new HashMap();
        List<Map<String,Object>> mapList=organService.getCompanys(uid,area,brand);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        map.put("dataList",mapList);
        return map;
    }

    /**
     * 获得当前登录用户，根据数据权限所分配的区域信息
     * @param uid
     * @param token
     * @return
     */
    @RequestMapping("/area/all")
    @ResponseBody
    public Map deptAreaAll(@RequestParam(name = "uid", defaultValue = "") String uid,
                           @RequestParam(name = "token", defaultValue = "") String token){
        Map map =new HashMap();
        map.put("success", true);
        map.put("msg", "区域查询成功！");
        map.put("dataList",TokenUtil.getDataArea(uid));
        return map;
    }

    /**
     * 获得当前登录用户，根据数据权限所分配的品牌信息
     * @param uid
     * @param token
     * @return
     */
    @RequestMapping("/brand/all")
    @ResponseBody
    public Map deptBrandAll(@RequestParam(name = "uid", defaultValue = "") String uid,
                           @RequestParam(name = "token", defaultValue = "") String token){
        Map map =new HashMap();
        map.put("success", true);
        map.put("msg", "品牌查询成功！");
        map.put("dataList",TokenUtil.getDataBrand(uid));
        return map;
    }

    @RequestMapping("/org")
    @ResponseBody
    public Map deptOrg(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token,
                    @RequestParam(name = "pId", defaultValue = "") String pId) throws Exception {
        Map map =new HashMap();
        List<Map<String,Object>> mapList=organService.getCompanyDept(pId);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        map.put("dataList",mapList);
        return map;
    }
    @RequestMapping("/cidAndcname")
    public Map getCidAndCname(@RequestParam(name = "uid", defaultValue = "") String uid,
                              @RequestParam(name = "token", defaultValue = "") String token) throws Exception{
        Map map = new HashMap();
        map.put("dataList",this.organService.getCNameAndCId());
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }

    @RequestMapping("/oCarBrand")
    public Map getOCarBrand(@RequestParam(name = "uid", defaultValue = "") String uid,
                              @RequestParam(name = "token", defaultValue = "") String token) throws Exception{
        Map map = new HashMap();
        map.put("dataList",this.organService.getOCarBrand());
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }

    /**
     * @Name: 图片保存到数据库
     * @Author: Liu XiaoLong
     * @Date: 2017/11/1 19:55
     * @Remark:
     */

    @RequestMapping("/writeImage")
    public Map writeImage(@RequestParam(name = "uid", defaultValue = "") String uid,
                          @RequestParam(name = "token", defaultValue = "") String token,
                          @RequestParam(name = "image", defaultValue = "") MultipartFile image,
                          @RequestParam(name = "time", defaultValue = "") String time,
                          @RequestParam(name = "id", defaultValue = "")  String id) throws Exception{
        Map map = new HashMap();
        organService.writePic(image,id);
        map.put("success", true);
        map.put("msg", "保存成功！");
        return map;
    }


    /**
     * @Name: 返回照片流
     * @Author: Liu XiaoLong
     * @Date: 2017/11/2 10:29
     * @Remark:
     */
    @RequestMapping("/readImage")
    public byte[] readImage(@RequestParam(name = "uid", defaultValue = "") String uid,
                            @RequestParam(name = "token", defaultValue = "") String token,
                            @RequestParam(name = "id", defaultValue = "") String id,
                            @RequestParam(name = "time", defaultValue = "") String time) throws Exception{
        byte[] b = organService.readPic(uid,id);
        return b;
    }

    /**
     * 获取门店信息
     * @param uid
     * @param token
     * @param storeParea
     * @param storeArea
     * @return
     * @throws Exception
     */
    @RequestMapping("/deptStoreArea")
    public Map deptStoreArea(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "storeParea", defaultValue = "") String storeParea,
                             @RequestParam(name = "storeArea", defaultValue = "") String storeArea,
                             @RequestParam(name = "type", defaultValue = "") String type) throws Exception {
        Map<String,Object> map=new HashMap<>();
        List list=organService.deptStoreArea("8a9aa28460026f310160059c8dd804b2",storeArea,storeParea,type);
        map.put("dataList",list);
        map.put("success",true);
        map.put("msg","查询成功!!");
        return map;
    }
    
    /**
     * @Name: 获取所有店
     * @Author: Liu XiaoLong
     * @Date: 2018/1/21 15:21
     * @Remark:
     */
    @RequestMapping("/AllDeptStore")
    public Map AllDeptStore(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token) throws Exception {
        Map<String,Object> map=new HashMap<>();
        List list=organService.AllDeptStore(uid);
        map.put("dataList",list);
        map.put("success",true);
        map.put("msg","查询成功!!");
        return map;
    }
}

